package com.example.thegreatwall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.thegreatwall.common.R;
import com.example.thegreatwall.entity.ArticleCollection;
import com.example.thegreatwall.service.ArticleCollectionService;
import org.apache.ibatis.annotations.Param;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin(origins = "*") //无论什么源都可以访问  不写这个不允许跨域
@RestController
@RequestMapping("/articleCollection")
public class ArticleCollectionController{
    @Autowired
    private ArticleCollectionService articleCollectionService;
    /**
     * 查询文章收藏状态
     */
    @GetMapping(value = "articlestatus")
    public R articlestatus(@RequestParam int userid){
        List<Integer> liststatus = articleCollectionService.aliststatus(userid);
        return new R().addDate("articlestatus",liststatus);
    }
    @GetMapping(value = "artall")
    public R artall(@RequestParam int userid,@RequestParam int articleid){
        int collect = articleCollectionService.isCollect(userid, articleid);
        if(collect==0){
            //这个人没收藏过这个文章
            ArticleCollection articleCollection = new ArticleCollection();
            articleCollection.setUserId(userid);  // 假设存在setUserid方法
            articleCollection.setArticleId(articleid);  // 假设存在setArticleId方法
            // 可以设置其他必要属性，如收藏时间等
            articleCollectionService.save(articleCollection);
            return new R().setMsg("收藏成功");
        }else {
            //取消这个收藏
            QueryWrapper<ArticleCollection> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userid);
            queryWrapper.eq("article_id", articleid);
            ArticleCollection one = articleCollectionService.getOne(queryWrapper);
            System.out.println(one);
            articleCollectionService.removeById(one.getArticleCollectionId());
            return new R().setMsg("取消收藏");
        }
    }
}